package com.sign.webview;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView _webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _webview = findViewById(R.id.webview);
        _webview.getSettings().setJavaScriptEnabled(true);
        _webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
        _webview.setWebViewClient(new WebViewClient());
        _webview.addJavascriptInterface(new Bridge(), "bridge");
        _webview.loadUrl("file:///android_asset/index.html");
        findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _webview.loadUrl("javascript:clear()");
            }
        });
        findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    System.out.println("============ 开始保存 ============");
                    _webview.evaluateJavascript("save()", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            System.out.println("============ 保存结束 ============" + Bridge.sb);
                            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                            intent.putExtra("url", String.valueOf(Bridge.sb));
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }

}
